<?php
/**
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/11/15
 * Time: 14:21
 */
class Controller_Admin_Channel extends Controller_Admin_Abstract
{
    private $DbServer = '';

    /**
     * 渠道配置列表
     */
    public function listAction()
    {
        global $_F;
        $page = FRequest::getInt('page');
        $domain = FRequest::getString('domain');
        $status = FRequest::getInt('status');
        $where =array();

        if($domain){
            $where['domain'] = array('like' => "%{$domain}%");
        }

        if($status){
            $where['status'] = $status;
        }


        $Table = new FTable('channel', '', $this->DbServer);
        $list = $Table->fields("*")
            ->where($where)
            ->order(array('id' => 'desc'))
            ->page($page)
            ->limit(30)
            ->select();
        $pagerInfo = $Table->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->assign('list', $list);
        $this->assign('channel_status', FConfig::get('type.channel_status'));
        $this->assign('domain', $domain);
        $this->display('admin/channel/list');
    }

    /**
     * 渠道配置修改
     */
    public function modifyAction(){
        global $_F;
        $channel_name = FRequest::getPostString('channel_name') ? FRequest::getPostString('channel_name') : '';
        $domain = FRequest::getPostString('domain') ? FRequest::getPostString('domain') : '';
        $status = FRequest::getPostInt('status') ? FRequest::getPostInt('status') : 1;
        $id = FRequest::getPostInt('id');
        $table = new FTable('channel', '', $this->DbServer);
        if($this->isPost()){
            $data = array(
                'channel_name' => $channel_name,
                'domain' => $domain,
                'status' => $status,
            );
            if($id){
                $result = $table->where("id={$id}")->save($data);
                if($result){
                    $this->success('修改成功', $_F['refer']);
                }else{
                    $this->error('修改失败', $_F['refer']);
                }
            }else{
                $table->insert($data);
                $this->success('添加成功', $_F['refer']);
            }
        }

        $id = FRequest::getInt('id');
        if($id){
            $table = new FTable('channel', '', $this->DbServer);
            $channelData = $table->where("id={$id}")->find();
            $this->assign('data', $channelData);
        }
        $this->assign('channel_status', FConfig::get('type.channel_status'));
        $this->display('admin/channel/modify');

    }

    /**
     * 状态修改
     */
    public function statusModifyAction(){
        global $_F;
        $status = FRequest::getInt('status');
        $id = FRequest::getInt('id');
        if(!$id || !$status){
            $this->error('参数为空', $_F['refer']);
        }
        $table = new FTable('channel', '', $this->DbServer);
        $result = $table->where(array('id' => $id))->save(array('status' => $status));
        if($result){
            $this->success('修改成功', $_F['refer']);
        }else{
            $this->error('修改失败', $_F['refer']);
        }
    }

    /**
     * 配置删除
     */
    public function deleteAction(){
        global $_F;
        $id = FRequest::getInt('id');
        if(!$id){
            $this->error('参数为空', $_F['refer']);
        }
        $table = new FTable('channel', '', $this->DbServer);
        $table->where("id={$id}")->remove(true);
        $this->success('删除成功', $_F['refer']);
    }

}
